﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using System.Web.Security;
public partial class admin_SearchResults : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["searchresult"];
        foreach (DataRow r in dt.Rows)
        {
            String workorder = r["workorder_reference_no"].ToString();
            string machineid=r["machine_id"].ToString();
            string date_submit = r["date_submitted"].ToString();
            string status = r["status"].ToString();
            int workorderID = Int32.Parse(r["workorder_id"].ToString());
            TableCell tc_workorder = new TableCell();
            tc_workorder.Text = workorder;
            tc_workorder.BorderWidth = 1;
            TableCell tc_datesubmit = new TableCell();
            tc_datesubmit.Text = date_submit;
            tc_datesubmit.BorderWidth = 1;
            TableCell tc_status = new TableCell();
            if (status == "1")
            {
                tc_status.Attributes.Add("backcolor","#00FA9A");
                tc_status.BackColor = System.Drawing.Color.Green;

            }
            else
            {

                tc_status.Attributes.Add("backcolor", "#FF0000");
                tc_status.BackColor = System.Drawing.Color.Red;

            }
         
            //tc_status.Text = status;
            tc_status.BorderWidth = 1;
            TableCell machinedetails = new TableCell();
            machinedetails.Text = GetMachineDetail(Int32.Parse(machineid));
            TableCell tc_view = new TableCell();
            LinkButton detailbutton = new LinkButton();
            detailbutton.Text = "View Details";
            detailbutton.PostBackUrl = "DetailView_Workorder.aspx?workorder_id=" + workorderID+"&machinename="+machinedetails.Text;
            tc_view.Controls.Add(detailbutton);
            TableRow tr = new TableRow();
        
            tr.Cells.Add(tc_workorder);
            tr.Cells.Add(machinedetails);
            tr.Cells.Add(tc_datesubmit);
            tr.Cells.Add(tc_status);
            tr.Cells.Add(tc_view);
            resultTable.Rows.Add(tr);

        }
    }
    public string GetMachineDetail(int machineid)
    {
        string machinedetails="";
        CreateConn obj = new CreateConn();
        string query = "SELECT machineName,siteName From Machines m JOIN Sites s on m.site_id=s.site_id WHERE machine_id='"+machineid+"'";
        
        
        DataTable dt = obj.SQLConnDataTable(query);
        foreach(DataRow dr in dt.Rows){
            machinedetails = dr["siteName"].ToString() + "-->" + dr["machineName"].ToString();
        }
    
        return machinedetails;
    }


    protected void backbuttonclicked(object sender, EventArgs e)
    {
        Server.Transfer("Search_Workorder.aspx");
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

}
